package com.xinruke.hostels.support;

import feign.form.spring.SpringFormEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.codec.Encoder;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication(scanBasePackages = "com.xinruke.hostels")
@EnableScheduling
@EnableEurekaClient
@EnableHystrix
@EnableFeignClients(basePackages = "com.xinruke.hostels")
@MapperScan(basePackages = {"com.xinruke.hostels.support.mapper","com.xinruke.hostels.common.mapper"})
public class SupportApplication {
    static Logger logger = LoggerFactory.getLogger(SupportApplication.class);


    @Value("${tmp.upload.path}")
    private String updateTempPath;

    @Value("${global.file.max.size}")
    private String fileMaxSize;

    public static void main(String[] args) {
        SpringApplication.run(SupportApplication.class, args);

        logger.info("--------------------------");
        logger.info("----Support Start Success----");
        logger.info("--------------------------");
    }

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver getCommonsMultipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        //resolveLazily属性启用是为了推迟文件解析，以在在UploadAction中捕获文件大小异常
        multipartResolver.setResolveLazily(true);
        multipartResolver.setMaxUploadSize(Integer.parseInt(fileMaxSize));
        multipartResolver.setMaxInMemorySize(Integer.parseInt(fileMaxSize));
        return multipartResolver;
    }

    @FeignClient(value = "upload-server", configuration = UploadService.MultipartSupportConfig.class)
    public interface UploadService {

        @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        String handleFileUpload(@RequestPart(value = "file") MultipartFile file);

        @Configuration
        class MultipartSupportConfig {
            @Bean
            public Encoder feignFormEncoder() {
                return new SpringFormEncoder();
            }
        }

    }

}
